package cn.mes.module.mes.service.basecustomerinfo;

import cn.mes.framework.common.pojo.PageResult;
import cn.mes.framework.common.util.object.BeanUtils;
import cn.mes.module.mes.controller.admin.basecustomerinfo.vo.BaseCustomerInfoPageReqVO;
import cn.mes.module.mes.controller.admin.basecustomerinfo.vo.BaseCustomerInfoSaveReqVO;
import cn.mes.module.mes.dal.dataobject.basecustomerinfo.BaseCustomerInfoDO;
import cn.mes.module.mes.dal.mysql.basecustomerinfo.BaseCustomerInfoMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;

import static cn.mes.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.mes.module.mes.enums.ErrorCodeConstants.BASE_CUSTOMER_INFO_NOT_EXISTS;

/**
 * 客户基础信息 Service 实现类
 *
 * @author 游创
 */
@Service
@Validated
public class BaseCustomerInfoServiceImpl implements BaseCustomerInfoService {

    @Resource
    private BaseCustomerInfoMapper baseCustomerInfoMapper;

    @Override
    public String createBaseCustomerInfo(BaseCustomerInfoSaveReqVO createReqVO) {
        // 插入
        BaseCustomerInfoDO baseCustomerInfo = BeanUtils.toBean(createReqVO, BaseCustomerInfoDO.class);
        baseCustomerInfoMapper.insert(baseCustomerInfo);
        // 返回
        return baseCustomerInfo.getId();
    }

    @Override
    public void updateBaseCustomerInfo(BaseCustomerInfoSaveReqVO updateReqVO) {
        // 校验存在
        validateBaseCustomerInfoExists(updateReqVO.getId());
        // 更新
        BaseCustomerInfoDO updateObj = BeanUtils.toBean(updateReqVO, BaseCustomerInfoDO.class);
        baseCustomerInfoMapper.updateById(updateObj);
    }

    @Override
    public void deleteBaseCustomerInfo(String id) {
        // 校验存在
        validateBaseCustomerInfoExists(id);
        // 删除
        baseCustomerInfoMapper.deleteById(id);
    }

    private void validateBaseCustomerInfoExists(String id) {
        if (baseCustomerInfoMapper.selectById(id) == null) {
            throw exception(BASE_CUSTOMER_INFO_NOT_EXISTS);
        }
    }

    @Override
    public BaseCustomerInfoDO getBaseCustomerInfo(String id) {
        return baseCustomerInfoMapper.selectById(id);
    }

    @Override
    public PageResult<BaseCustomerInfoDO> getBaseCustomerInfoPage(BaseCustomerInfoPageReqVO pageReqVO) {
        return baseCustomerInfoMapper.selectPage(pageReqVO);
    }

}